#include<bits/stdc++.h>
using namespace std;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
const int N = 310;

int n;
int s[N];
int f[N][N];//从i到j合并到一起的最小步骤

int main()
{
    close();
    cin>>n;
    for (int i = 1; i <= n; i ++ ) cin>>s[i];

    for (int i = 1; i <= n; i ++ ) s[i] += s[i - 1];

    for (int len = 2; len <= n; len ++ )//枚举区间长度,这个len把左端点也算进去的所以是小于等于n
    {
        for (int i = 1; i + len - 1 <= n; i ++ )//枚举左端点
        {
            int l = i, r = i + len - 1;//从i到 i+len-1长度为 len
            f[l][r] = 1e8;

            for (int k = l; k < r; k ++ )  f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);

        }
    }

    cout<<f[1][n]<<'\n';
    return 0;
}

